package com.hd123.rumba.gwt.widget2.client.fielddef;

/**
 * 字符串类型字段的定义
 * 
 * @author LiQi
 * 
 */
public class StringFieldDef extends FieldDef {

  /** 最小长度，实际长度应>=最小长度 */
  protected int minLength = 0;

  /** 最大长度，实际长度应<=最大长度 */
  protected int maxLength = 50;

  /** 模式，用正则表达式表示 */
  protected String pattern;
  protected String patternPrompt;

  public StringFieldDef(String name, String caption) {
    super(name, caption, true);
  }

  public StringFieldDef(String name, String caption, boolean nullable, int minLength,
      int maxLength, String pattern, String patternPrompt) {
    super(name, caption, nullable);
    this.minLength = minLength;
    this.maxLength = maxLength;
    this.pattern = pattern;
    this.patternPrompt = patternPrompt;
  }

  public StringFieldDef(String name, String caption, boolean nullable, int minLength, int maxLength) {
    super(name, caption, nullable);
    this.minLength = minLength;
    this.maxLength = maxLength;
  }

  public FieldDef clone() {
    return new StringFieldDef(name, caption, nullable, minLength, maxLength, pattern, patternPrompt);
  }

  public boolean validateText(String text, StringBuffer message) {
    return validate(text, message, true);
  }

  public boolean validateText(String text, StringBuffer message, boolean checkNullable) {
    return validate(text, message, checkNullable);
  }

  public boolean validate(String value, StringBuffer message) {
    return validate(value, message, true);
  }

  /**
   * 验证传入的值是否合法
   * 
   * @param value
   * @return
   */
  public boolean validate(String value, StringBuffer message, boolean checkNullable) {
    value = "".equals(value) ? null : value;
    boolean result = super.validate(value, message, checkNullable);
    if (value == null)
      return result;

    // minLength
    if (value.length() < minLength) {
      appendMessage(message, "长度应该大于等于" + minLength);
      result = false;
    }

    // maxLength
    if (value.length() > maxLength) {
      appendMessage(message, "超过最大长度" + maxLength);
      result = false;
    }

    // pattern
    if (pattern != null) {
      if (!matchRegEx(value, pattern)) {
        appendMessage(message, "格式不合法，" + patternPrompt);
        result = false;
      }
    }

    finalizeMessage(message);
    return result;
  }

  public static native boolean matchRegEx(String value, String pattern) /*-{
        var re = new RegExp(pattern);
        return re.test(value);
      }-*/;

  public int getMinLength() {
    return minLength;
  }

  public void setMinLength(int minLength) {
    this.minLength = minLength;
  }

  public int getMaxLength() {
    return maxLength;
  }

  public void setMaxLength(int maxLength) {
    this.maxLength = maxLength;
  }

  public String getPattern() {
    return pattern;
  }

  public void setPattern(String pattern) {
    this.pattern = pattern;
  }

  public String getPatternPrompt() {
    return patternPrompt;
  }

  public void setPatternPrompt(String patternPrompt) {
    this.patternPrompt = patternPrompt;
  }

}
